package com.android.launcher3.hotseat;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.android.launcher3.AppInfo;
import com.android.launcher3.R;
import com.android.launcher3.Utilities;
import com.android.launcher3.touch.ItemClickHandler;
import com.android.launcher3.widget.HotSeatAppWidget;
import com.x.component.framework.ActivityManagerWrapper;

import java.util.ArrayList;
import java.util.List;

public class CustomHotSeat extends FrameLayout {

    public static int CUSTOM_HOT_SEAT_HEIGHT = 98;
    public static int CUSTOM_HOT_SEAT_STRIP_SCREEN_HEIGHT = 60;

    private LinearLayout customAppContainer;
    private List<HotSeatAppWidget> apps = new ArrayList<>();

    private OnItemClickListener onItemClickListener;

    private ImageView mRecentApp1;
    private ImageView mRecentApp2;

    private View allApp;
    private View googleVoice;

    private LinearLayout container;

    public CustomHotSeat(@NonNull Context context) {
        super(context);
        init();
    }

    public CustomHotSeat(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public CustomHotSeat(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    void init() {
        LayoutInflater.from(getContext()).inflate(R.layout.layout_custom_hot_seat, this, true);
        container = findViewById(R.id.container);
        customAppContainer = findViewById(R.id.custom_app_container);
        mRecentApp1 = findViewById(R.id.v_recent_app1);
        mRecentApp2 = findViewById(R.id.v_recent_app2);
        allApp = findViewById(R.id.v_all_app);
        mRecentApp1.setOnClickListener(v -> {
            if (mRecentApp1.getTag() != null) {
                ItemClickHandler.INSTANCE.onClick(mRecentApp1);
            }
        });

        mRecentApp2.setOnClickListener(v -> {
            if (mRecentApp2.getTag() != null) {
                ItemClickHandler.INSTANCE.onClick(mRecentApp2);
            }
        });

        //计算剩余空间可以添加多少个App
        DisplayMetrics dm = getResources().getDisplayMetrics();
        int width = dm.widthPixels;
        Log.d("TinkerLauncher3", "hotseat width: " + dm.widthPixels);
        Log.d("TinkerLauncher3", "hotseat height: " + dm.heightPixels);

        boolean isStripScreen = Utilities.isLongStripScreen(getContext());

        //两个图标各50 左右padding各10 两根线各1 间距40 * 2
        int customAppContainerWidget = width - Utilities.pxFromDp(isStripScreen ? 334 : 392, dm);

        //每个图标60大小加左右间距20 80宽度


        int customAppNum = customAppContainerWidget / Utilities.pxFromDp(isStripScreen ? 85 : 100, dm);

        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(Utilities.pxFromDp(isStripScreen ? 45 : 60, dm), Utilities.pxFromDp(isStripScreen ? 45 : 60, dm));

        layoutParams.setMargins(Utilities.pxFromDp(20, dm), 0, Utilities.pxFromDp(20, dm), 0);

        for (int i = 0; i < customAppNum; i++) {
            HotSeatAppWidget hotSeatAppWidget = new HotSeatAppWidget(getContext());
            apps.add(hotSeatAppWidget);
            customAppContainer.addView(hotSeatAppWidget, layoutParams);
            final int ii = i;
            hotSeatAppWidget.setForeground(getContext().getResources().getDrawable(R.drawable.recommend_widget_dpad_selector));
            hotSeatAppWidget.setOnClickListener(v -> {
                if (onItemClickListener != null && hotSeatAppWidget.getAppInfo() == null) {
                    onItemClickListener.onItemClick(v, ii);
                    return;
                }

                if (hotSeatAppWidget.getAppInfo() != null) {
                    ItemClickHandler.INSTANCE.onClick(hotSeatAppWidget);
                }

            });

            hotSeatAppWidget.setOnLongClickListener(v -> {

                if (hotSeatAppWidget.getTag() != null) {
                    hotSeatAppWidget.setAppInfo(null);

                    onItemClickListener.onItemLongClick(v, ii);
                }
                return true;
            });

        }
        googleVoice = findViewById(R.id.v_google);

        googleVoice.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                Log.d("TinkerVoice", "onClick");
                ActivityManagerWrapper.getInstance().startGoogleVoiceAssistant();
            }
        });
        screenAdaption();
    }

    void screenAdaption() {

        if (!Utilities.isLongStripScreen(getContext())) return;
        DisplayMetrics displayMetrics = getResources().getDisplayMetrics();
        LayoutParams layoutParams = (LayoutParams) container.getLayoutParams();
        layoutParams.height = Utilities.pxFromDp(CUSTOM_HOT_SEAT_STRIP_SCREEN_HEIGHT, displayMetrics);
        container.setLayoutParams(layoutParams);
        container.setBackgroundResource(R.drawable.bg_corners_black_small);

        LinearLayout.LayoutParams recentApp1 = (LinearLayout.LayoutParams) mRecentApp1.getLayoutParams();
        recentApp1.width = Utilities.pxFromDp(35, displayMetrics);
        recentApp1.height = Utilities.pxFromDp(35, displayMetrics);
        mRecentApp1.setLayoutParams(recentApp1);

        LinearLayout.LayoutParams recentApp2 = (LinearLayout.LayoutParams) mRecentApp2.getLayoutParams();
        recentApp2.width = Utilities.pxFromDp(35, displayMetrics);
        recentApp2.height = Utilities.pxFromDp(35, displayMetrics);
        mRecentApp2.setLayoutParams(recentApp2);

        LinearLayout.LayoutParams allAppLayoutParams = (LinearLayout.LayoutParams) allApp.getLayoutParams();
        allAppLayoutParams.width = Utilities.pxFromDp(35, displayMetrics);
        allAppLayoutParams.height = Utilities.pxFromDp(35, displayMetrics);
        allApp.setLayoutParams(allAppLayoutParams);

        LinearLayout.LayoutParams googleVoiceLayoutParams = (LinearLayout.LayoutParams) googleVoice.getLayoutParams();
        googleVoiceLayoutParams.width = Utilities.pxFromDp(35, displayMetrics);
        googleVoiceLayoutParams.height = Utilities.pxFromDp(35, displayMetrics);
        googleVoice.setLayoutParams(googleVoiceLayoutParams);

    }


    public OnItemClickListener getOnItemClickListener() {
        return onItemClickListener;
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    ///传入空就是清除该App
    public void setAppInfo(int position, @Nullable AppInfo appInfo, boolean needSava) {
        if (position >= 0 && position < apps.size()) {
            apps.get(position).setTag(appInfo);
            apps.get(position).setAppInfo(appInfo);
            if (needSava) {
                String packageName = appInfo != null && appInfo.componentName != null ? appInfo.componentName.getPackageName() : null;
                CustomHotSeatManager.INSTANCE.saveHotSeat(new CustomHotSeatManager.HotSeatEntity(position, packageName));
            }
        }
    }

    ///传入空就是清除App
    public void setRecentTaskApp(@Nullable AppInfo recent1, @Nullable AppInfo recent2) {

        if (recent1 != null) {
            mRecentApp1.setTag(recent1);
            if (recent1.iconBitmap != null) {
                mRecentApp1.setImageBitmap(recent1.iconBitmap);
            }
            mRecentApp1.setVisibility(VISIBLE);
        } else {
            mRecentApp1.setVisibility(INVISIBLE);
        }

        if (recent2 != null) {
            mRecentApp2.setTag(recent2);
            if (recent2.iconBitmap != null) {
                mRecentApp2.setImageBitmap(recent2.iconBitmap);
            }
            mRecentApp2.setVisibility(VISIBLE);
        } else {
            mRecentApp2.setVisibility(INVISIBLE);
        }
    }

    public void setAllAppClick(OnClickListener onClickListener) {
        allApp.setOnClickListener(onClickListener);
    }


    public interface OnItemClickListener {
        void onItemClick(View view, int position);

        void onItemLongClick(View view, int position);
    }


}
